<?php
// application/controllers/LetterController.php

/**
 * LetterController is the controller for handling letters
 *

 * @author Hermen van den Hoorn
 * @version $Id$
 */
class LetterController extends Vakanz_CRM_Controller_Adminaction
{
    /**
     * The "index" action is the default action for all controllers.
     *
     * Assuming the default route and default router, this action is dispatched
     * via the following urls:
     *   /letter/
     *   /realation/index
     *
     * @return void
     */
	public function indexAction() {
		$letters =new Vakanz_Letters();
		$lettersRs = $letters->fetchAll();
		$this->view->letters = $lettersRs->toStdObjectArray();
	}

	public function lasteditedAction() {
		$letters =new Vakanz_Letters();
		$lettersRs = $letters->fetchAll($letters->select()->order('letter_valid_from DESC')->limit(10));
		$this->view->letters = $lettersRs->toStdObjectArray();
	}
    public function newAction()
    {
        /*
           There is nothing inside this action, but it will still attempt to
           render a view.  This is because by default, the front controller
           uses the ViewRenderer action helper to handle auto rendering
           In the MVC grand scheme of things, the ViewRenderer allows us to
           draw the line between the C and V in the MVC.  Also note this action
           helper is optional, but on default.
        */
        $this->_forward('edit');
    }
    public function editAction() {
    	$letters = new Vakanz_Letters();
    	if ($this->_getParam('letter_id')) {
    		$this->view->letter = $letters->fetchAll($letters->select()->where('letter_id = ?', $this->_getParam('letter_id')))->current();
    	} else {
    		$this->view->letter = $letters->createRow();
    	}
    	$letter = $letters->createRow();
		$forms = new General_Forms();
		$form = $forms->findByName('letter');
		$form->getFormElements();
		$formObj = $form->toStdObject();
        $this->view->form = $formObj;


    }
    public function searchAction() {
    	$this->render($this->getRequest()->getActionName(),$this->getRequest()->getActionName());
    }

    public function checkAction() {
    	$letters = new Vakanz_Letters();
    	if ($this->_getParam('letter_id') == 0) {
    		$letter = $letters->createRow();
    	} else {
    		$query = $letters->select()->where('letter_id = ? ',$this->_getParam('letter_id'));
    		$letter = $letters->fetchAll($query)->current();
    	}
    	$letter->fillByParams($this->_request->getParams());
    	if ($this->_getParam('letter_id') == 0) {
	    	if ($letter->isExisting()) {
	    		$existing = $letter->getExistingLetter();
	    		$this->view->letter = $letter;
	    		$this->view->existing_letter = $letter;
	    	} else {
	    		$this->_forward('save');
	    	}
    	} else {
    		$this->_forward('save');
    	}

    }

    public function saveAction() {
    	$letters = new Vakanz_Letters();
    	if ($this->_getParam('letter_id') == 0) {
    		$letter = $letters->createRow();
    	} else {
    		$query = $letters->select()->where('letter_id = ? ',$this->_getParam('letter_id'));
    		$letter = $letters->fetchAll($query)->current();
    	}
    	$letter->fillByParams($this->_request->getParams());
    	$letter->save();
    }
}
